/*
 * @Title: 票选优品首页
 * @Author: Jcuiweif
 * @Date:   2018-12-19
 * @Last Modified by:   Jcuiweif
 * @Last Modified time: 2018-12-19
 */
$(document).ready(function(){
  if ($(document).width()<=450){
    var t = $(document).width()/10;
    $("html").css("font-size",t);
    } else {
      $("html").css("font-size",43);
    }
  })
var dataInfo = {},
  template = {
    init: function() {
      Vue.use(VueLazyload, {
        preLoad: 1.3,
        error: 'https://h5.meetao.com/img/product/init.png',
        loading: 'https://h5.meetao.com/img/product/init.png',
        attempt: 1
      });
      dataInfo.init = new Vue({
        el: '#app',
        data: {
          width: document.body.offsetWidth,
          tabData: [],
          swiperData: [],
          votebannerdown:[],
          versions: versions,
          icon: {
            normal: '../img/index/shouhui.png',
            active: '../img/index/shouhong.png',
            normal1: '../img/index/gouhui.png',
            active1: '../img/index/gouhong.png',
            active2: '../img/index/myhong.png',
            normal2: '../img/index/myhui.png',
            active3: '../img/index/pthong.png',
            normal3: '../img/index/pthui.png',
            active4:"../img/index/votegoods.png",
            normal4:"../img/index/votegoodsgray.png",
          },
          playicon:'../img/playVideo.png',
          eyes:'../img/voteGoods/eyes.png',
          whtiteeyes:'../img/voteGoods/whtiteeyes.png',
          goodsinfo:'../img/voteGoods/newgoods.png',
          selected:'../img/voteGoods/selected.png',
          noselected:'../img/voteGoods/noselected.png',
          active:3,
          activeindex:0,
          all: false, //loadOver - show
          all1: false,
          isFx: 1,
          pagesource:1,
          pagenew:1,
          isDz: 0,
          page: 1,
          choose:0,
          limit: 10,
          loading: false,
          isLoading: false,
          finished: false,
          loading1: false,
          isLoading1: false,
          finished1: false,
          areanone:'../img/voteGoods/area.png',
          listData: [], //商品集合
          navGroup:['又上新啦','票选优品','溯源'],
          newgoods:[],
          share:{
            img:'../img/voteGoods/logo100.png',
            noneimg: '../img/404/404.png'
          },
          votedetails:[],
          backsource:[]
        },
        methods: {
          onLoad: function() {
              dataInfo.init.sourceagain();
          },
          onLoad1: function() {
              dataInfo.init.newgooodsagain();
          },
          onRefresh: function() { //下拉刷新 
              this.backsource = [];
              this.pagesource = 1;
            dataInfo.init.all = false;
            setTimeout(function() {
                dataInfo.init.sourceagain();
            }, 500);
          },
          onRefresh1: function() { //下拉刷新 
              this.newgoods = [];
             this.pagenew = 1;
            dataInfo.init.all1 = false;
            setTimeout(function() {
                dataInfo.init.newgooodsagain();
            }, 500);
          },
          newgooodsagain:function(){
            ajaxTemplate(addressTest + SERVICE.votenewgoods, function(data) {
              console.log('wewrewr',data);
              var daTa = data.data.result;
              if (!daTa||daTa.length == 0){
                // dataInfo.init.$toast('全部加载完');
                if (dataInfo.init.newgoods.length ==0){
                  // dataInfo.init.nodata= true;
                } else {
                  dataInfo.init.all1= true;
                }
                dataInfo.init.loading1 = false;
                dataInfo.init.isLoading1 = false;
                dataInfo.init.finished1 = true;
                // dataInfo.init.all = true;
                return;
              }
              if (data.tag == 0) {
                dataInfo.init.newgoods = JSON.parse(JSON.stringify(dataInfo.init.newgoods.concat(daTa)));
                if (daTa.length < dataInfo.init.limit) {
                  console.log("无数据")
                  dataInfo.init.finished1 = true;
                  dataInfo.init.all1 = true;
                } else {
                  console.log('有数据')
                  dataInfo.init.finished1 = false;
                }
                dataInfo.init.loading1 = false;
                dataInfo.init.isLoading1 = false;
              } else {
                dataInfo.init.$toast(data.errMsg);
                if (data.errMsg == '未查询到符合条件的商品'){
                  dataInfo.init.loading1 = false;
                  dataInfo.init.isLoading1 = false;
                  dataInfo.init.all1 = true;
                  dataInfo.init.finished1 = true;
                }
              };
            }, {
              limit: dataInfo.init.limit,
              page: dataInfo.init.pagenew++,
            });
          },
          sourceagain:function(){
            ajaxTemplate(addressTest + SERVICE.backsource, function(data) {
              console.log('溯源图',data);
              var daTa = data.data.result;
              // dataInfo.init.backsource = data.data.result;//
              // var s = dataInfo.init.pagesource++;
              // dataInfo.init.$set(dataInfo.init,'pagesource',s);
              if (!daTa||daTa.length == 0){
                // dataInfo.init.$toast('全部加载完');
                if (dataInfo.init.backsource.length ==0){
                  // dataInfo.init.nodata= true;
                } else {
                  dataInfo.init.all= true;
                }
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;
                dataInfo.init.finished = true;
                // dataInfo.init.all = true;
                return;
              }
              if (data.tag == 0) {
                dataInfo.init.backsource = JSON.parse(JSON.stringify(dataInfo.init.backsource.concat(daTa)));
                if (daTa.length < dataInfo.init.limit) {
                  console.log("无数据")
                  dataInfo.init.finished = true;
                  dataInfo.init.all = true;
                } else {
                  console.log('有数据')
                  dataInfo.init.finished = false;
                };
                if (dataInfo.init.backsource.length<10){
                  dataInfo.init.finished = true;
                  dataInfo.init.all = true;
                }
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;
              } else {
                dataInfo.init.$toast(data.errMsg);
                if (data.errMsg == '未查询到符合条件的商品'){
                  dataInfo.init.loading = false;
                  dataInfo.init.isLoading = false;
                  dataInfo.init.all = true;
                  dataInfo.init.finished = true;
                }
              };
            }, {
              limit: dataInfo.init.limit,
              page: dataInfo.init.pagesource++,
            });
          },
          bannerJump: function(data) { //bannerJump
            if (data.linkRemark == 'product') { //商品 文章 视频 均为跳转内部地址 h5为跳转全链接地址
              jumpProductDetails(data.params);
            } else if (!!data.link && data.link !== '$[link]'&& data.link !== '${link}') {
              window.location.href = data.link;
            };
          },
          bannerJump2: function(data,index) { //bannerJump
            console.log(data,index,data.linkRemark)
            // window.location.href = data.h5JumpUrl;productDetails.html?isFx=1&isPx=1&productId=P631998productId为商品id
            if (index === 0){
              location.href = 'FPtAgainNew.html'
            } else if (index === 1){
              location.href = 'FPtVoteProducts.html'
            } else if (index === 2){
              location.href = 'FPtBackSource.html'
            }
          },
          toProduct: function(data) { //toProduct
            jumpProductDetails(data);
          },
          textsource: function(index){
            var sourcegroup = dataInfo.init.backsource;
            var sourceCode = sourcegroup[index].sourceCode;
            var type = sourcegroup[index].sourceType;
            console.log(sourcegroup[index]);
            ajaxTemplate(addressTest + SERVICE.sourceinfo, function(data) {
              if (data.tag == 0) {
                  var result = data.data.result;
                  // console.log('theme',result.isOnline);
                  if (result.isOnline == 1){
                    if (type == 'text'){
                      location.href = 'FPtSourceTalk.html?sourceCode='+encodeURIComponent(sourceCode)+'&sourceTitle='+encodeURIComponent(sourcegroup[index].sourceTitle)+'&id='+encodeURIComponent(sourcegroup[index].id);
                    } else if (type == 'video'){
                      location.href = 'FPtVoteVedioDetail.html?sourceCode='+encodeURIComponent(sourceCode)+'&sourceTitle='+ encodeURIComponent(sourcegroup[index].sourceTitle)+'&id='+encodeURIComponent(sourcegroup[index].id);
                    }
                  } else {
                    dataInfo.init.$toast('该活动已下线');
                    dataInfo.init.backsource = dataInfo.init.backsource.splice(index,1);
                  }
                  
              } else {
                  dataInfo.init.$toast(data.errMsg);
              };
              }, {
                  sourceCode:sourceCode,
              });
          },
          newgoodstab:function(){
            var index = dataInfo.init.activeindex;
            if (index == '0'){
            } else if (index == '1'){
              
            } else if (index == '2'){
              
            }
          },
          prodect:function(index){
            // dataInfo.init.newgoods[index]
            // console.log(dataInfo.init.newgoods[index])
            console.log(dataInfo.init.newgoods[index].fxVirtualGoodsCode,dataInfo.init.newgoods[index].voteActivityIssueNum,dataInfo.init.newgoods[index].voteItemCode)
            if(dataInfo.init.newgoods[index].id){
              location.href ="productDetails.html?isFx=1&isPx=1&productId="+encodeURIComponent(dataInfo.init.newgoods[index].fxVirtualGoodsCode)+'&voteItemNum'+encodeURIComponent(dataInfo.init.newgoods[index].voteActivityIssueNum)+'&voteItemCode='+encodeURIComponent(dataInfo.init.newgoods[index].voteItemCode); ; 
            }
          }, 
          tabJump: function(data) {
            location.href = "../html/productClassify.html?code=" + data.code + "&name=" + escape(data.name);
          },
          getProductDetails: function() { //获取商品数据
            ajaxTemplate(addressUrl + SERVICE.queryFxGoodsList, function(data) {
              console.log(data);
              var daTa = data.data.result;
              if (data.tag == 0) {
                dataInfo.init.listData = JSON.parse(JSON.stringify(dataInfo.init.listData.concat(daTa)));
                if (daTa.length < dataInfo.init.limit) {
                  console.log("无数据")
                  dataInfo.init.finished = true;
                  dataInfo.init.all = true;
                } else {
                  console.log('有数据')
                  dataInfo.init.finished = false;
                };
                dataInfo.init.loading = false;
                dataInfo.init.isLoading = false;
                //					是否要有秒杀
                //					daTa.forEach(function(item, index) {
                //						if(item.isKill !== 0) {
                //							dataInfo.init.listData.push(item);
                //						};
                //					});
                //					setTimeout(function() {
                //						var tid = "index" + (daTa.length - 1);
                //						$('#' + tid).ready(function() {
                //							$(".loadImgBox").addClass("rgba").hide();
                //						});
                //					}, 300);
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              limit: dataInfo.init.limit,
              page: dataInfo.init.page++,
            });
          },
          voteactive: function(ind,event){//投票
            var votedetails = dataInfo.init.votedetails;
            console.log(votedetails)
          },
          votedetailEvent:function(index,event){
            // if (event.target.innerHTML != '投票'){
              var votedetails = dataInfo.init.votedetails;
              location.href='FPtVoteDetails.html?voteItemCode='+encodeURIComponent(votedetails[index].code);
            // }
            // 
          },
          getBanner: function() {
            ajaxTemplate(addressUrl + SERVICE.votegoodsbanner, function(data) {//获取头部banner
              console.log('banner',data);
              if (data.tag == 0) {
                dataInfo.init.swiperData = data.data.result;
              } else {
                dataInfo.init.$toast(data.errMsg);
              };
            }, {
              bannerCode: 'VOTE_ACTIVITY_INDEX'
            });
            ajaxTemplate(addressTest + SERVICE.votebannerdown, function(data) {//获取头部banner下part图
                console.log('part图',data);
                if (data.tag == 0) {
                  dataInfo.init.votebannerdown = data.data.result;
                } else {
                  dataInfo.init.$toast(data.errMsg);
                };
              }, {
                
              });
              // ajaxTemplate(addressTest + SERVICE.votegoodslist, function(data) {//投票优品
              //   // console.log("wewr",data);
              //   if (data.tag == 0) {
              //     dataInfo.init.votedetails = data.data.result.list;
              //   } else {
              //     dataInfo.init.$toast(data.errMsg);
              //   };
              // }, {
              //   userId:userId
              // });

              ajaxTemplate(addressTest + SERVICE.votegoodslist, function(data) {//投票优品
                console.log('票选优品',data);
                if (data.tag == 0) {
                  // dataInfo.init.votedetails = data.data.result.list;
                  dataInfo.init.$set(dataInfo.init,'votedetails',data.data.result.list);
                } else {
                  dataInfo.init.$toast(data.errMsg);
                };
              }, {
                userId:userId
              });
              dataInfo.init.onLoad();
          },
        },
      });
      dataInfo.init.getBanner();
    //   dataInfo.init.getTab();
    }
  };
template.init();